package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.PmsProCateWithChirendDTO;
import com.tulingxueyuan.mall.dto.PmsProductCategoryDTO;
import com.tulingxueyuan.mall.dto.ProductCategoryDTO;
import com.tulingxueyuan.mall.modules.pms.model.PmsProduct;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author XuShu
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
    @Autowired
    private PmsProductCategoryService productCategoryService;

    @ApiOperation("商品列表")
    @GetMapping(value = "/list/{paraId}")
    public CommonResult<CommonPage<PmsProductCategory>> list(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
            @PathVariable("paraId") Integer paraId){
        Page page =  productCategoryService.listPage(pageNum,pageSize,paraId);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("修改导航栏状态")
    @PostMapping(value = "/update/navStatus")
    public CommonResult updateNavStatus(
            @RequestParam(value = "ids",required = false) Integer[] ids,
            @RequestParam(value = "navStatus",required = false) Integer navStatus){
        boolean b = productCategoryService.updateNavStatus(ids, navStatus);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }
    @ApiOperation("是否显示")
    @PostMapping(value = "/update/showStatus")
    public CommonResult updateShowStatus(
            @RequestParam(value = "ids",required = false) Integer[] ids,
            @RequestParam(value = "showStatus",required = false) Integer showStatus){
        boolean b = productCategoryService.updateShowStatus(ids, showStatus);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }
    @ApiOperation("是否显示")
    @PostMapping(value = "/delete/{id}")
    public CommonResult delete(@PathVariable("id") Integer id){
        if (id != null){
            boolean b = productCategoryService.removeById(id);
            return b?CommonResult.success(b):CommonResult.failed();
        }else {
            return CommonResult.failed("该id不存在");
        }
    }
    @ApiOperation("添加分类")
    @PostMapping(value = "/create")
    public CommonResult delete(@RequestBody ProductCategoryDTO productCategoryDTO){
        if (productCategoryDTO != null){
            boolean b = productCategoryService.saveWithAttrreLation(productCategoryDTO);
            return b?CommonResult.success(b):CommonResult.failed();
        }else {
            return CommonResult.failed("添加失败");
        }
    }
    @ApiOperation("根据id查找分类")
    @GetMapping(value = "/{id}")
    public CommonResult getCreate(@PathVariable("id") Integer id){
        if (id != null){
            PmsProductCategory productCategory = productCategoryService.getById(id);
            return CommonResult.success(productCategory);
        }else {
            return CommonResult.failed("该id不存在");
        }
    }
    @ApiOperation("修改分类")
    @PostMapping(value = "/update/{id}")
    public CommonResult update(@PathVariable("id") Integer id,@RequestBody PmsProductCategoryDTO pmsProductCategoryDTO){
        if (id != null){
            boolean b = productCategoryService.editWithAttrbibute(pmsProductCategoryDTO);
            return b?CommonResult.success(b):CommonResult.failed();
        }else {
            return CommonResult.failed("该id不存在");
        }
    }

    @ApiOperation("获取分类列表包括children")
    @GetMapping(value = "/list/withChildren")
    public CommonResult<List<PmsProCateWithChirendDTO>> getWithChildrenList(){
        List<PmsProCateWithChirendDTO> list =  productCategoryService.getWithChildrenList();
        return CommonResult.success(list);
    }
}

